package com.firstclass.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.firstclass.dao.TakecourseDao;
import com.firstclass.entity.TakecourseEntity;
import com.firstclass.service.TakecourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("takecourseService")
public class TakecourseServiceImpl extends ServiceImpl<TakecourseDao, TakecourseEntity> implements TakecourseService {
    @Autowired
    private TakecourseDao takecourseDao;

    @Override
    public List<TakecourseEntity> queryByCondition(String name, String num, String grade) {
        Map<String, Object> condition = new HashMap<>(4);
        if (StrUtil.isNotBlank(name)) {
            condition.put("name", name);
        }
        if (StrUtil.isNotBlank(num)) {
            condition.put("num", num);
        }
        if (StrUtil.isNotBlank(grade)) {
            condition.put("grade", grade);
        }
        return takecourseDao.selectByCondition(condition);
    }

    @Override
    public void updateStatusById(Integer id, Integer flag) {
        Integer status = flag == 1 ? 0 : 1;
        takecourseDao.updateStatus(id, status);
    }

    @Override
    public boolean save(TakecourseEntity takecourseEntity) {
        if (takecourseEntity.getId() == null) {
            takecourseEntity.setStatus(0);
            takecourseDao.insert(takecourseEntity);
        } else {
            takecourseDao.updateById(takecourseEntity);
        }
        return true;
    }

}
